package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.bwie.pojo.R;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: zyc
 * @create: 2024-07-18 16:25
 **/
@RestController
@RequestMapping("business/brand")
public class BrandController {
    @Resource
    TbBrandService tbBrandService;

    /**
     * 列表数据展示方法
     * @param
     * @return
     */
    @RequestMapping("list")
    public R list(@RequestBody TbBrand tbBrand){
        LambdaQueryWrapper<TbBrand> queryWarrper = new LambdaQueryWrapper<TbBrand>();
        if(StringUtils.isNotBlank(tbBrand.getName())){
            queryWarrper.like(TbBrand::getName,tbBrand.getName());
        }
        // 做相等于的查询，全文匹配成功后返回
        if(StringUtils.isNotBlank(tbBrand.getFirstChar())){
            queryWarrper.eq(TbBrand::getFirstChar,tbBrand.getFirstChar());
        }

        Page<TbBrand> pagedb = new Page<TbBrand>(tbBrand.getStartPage(),tbBrand.getEndPage());
        Page<TbBrand> pageResult = tbBrandService.page(pagedb, queryWarrper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }



    @RequestMapping("findById")
    public R findById(@RequestParam("id")Long id){

        return R.ok(tbBrandService.getById(id));
    }

    /**
     * 根据id进行删除0
     * @param
     * @return
     */
    @RequestMapping("delById")
    public R delById(@RequestParam("id")Long id){
        return R.ok(tbBrandService.removeById(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delByIds")
    public R delByIds(@RequestBody Integer[] ids){

        return R.ok(tbBrandService.removeByIds(Arrays.asList(ids)));
    }

    @RequestMapping("update")
    public R update(@RequestBody TbBrand brand){
        return R.ok(tbBrandService.updateById(brand));

    }

    @RequestMapping("insert")
    public R insert(@RequestBody TbBrand brand){
        return R.ok(tbBrandService.save(brand));

    }


    public static void main(String[] args) {
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

        //添加数据
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", 1);
        map1.put("name", "张三");
        map1.put("age", 10);
        dataList.add(map1);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", 2);
        map2.put("name", "李四");
        map2.put("age", 40);
        dataList.add(map2);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", 1);
        map3.put("name", "张三");
        map3.put("age", 10);
        dataList.add(map3);

//        // 循环
//        dataList.stream().forEach(map -> {
//            String name = (String) map.get("name");
//            System.out.println(name);
//        });
//        // 过滤
//        List<Map<String, Object>> ageList = dataList.stream().filter(map -> {
//            return (Integer) map.get("id") == 1;
//        }).collect(Collectors.toList());
//
//        System.out.println(ageList);

        // 过滤
        List<Map<String, Object>> ageList = dataList.stream()
                .filter(map -> (Integer) map.get("id") == 1).collect(Collectors.toList());

        System.out.println(ageList);


        // 去重
        // select distict name from table
//        System.out.println(dataList);
//        List<Map<String, Object>> collect = dataList.stream().distinct().collect(Collectors.toList());
//        System.out.println(collect);

        // 转map后，只要某个或某些数据
//        List<Integer> idList = dataList.stream().map(map -> {
//            return (Integer) map.get("id");
//        }).collect(Collectors.toList());
//        System.out.println(idList);


//        List<Integer> idList = dataList.stream()
//                .map(map -> (Integer) map.get("id"))
//                .collect(Collectors.toList());
//        System.out.println(idList);
    }
}
